import { customForm } from "@/components/ProForm/interface";
import { customTable } from "@/components/ProTable/interface";
import { TreeDataProp } from "./treeData";
import { Dictionary } from "@/api/interface/auth/basic";

export function useDataGrid(refresh: () => Promise<void>, addForm: customForm<Dictionary.ReqSave>, treeData: TreeDataProp) {
  let dataGrid = reactive<customTable>({
    initParam: {
      type: ""
    },
    data: [],
    // 表格配置项
    columns: [
      { prop: "operation", label: "operation", fixed: "left", width: 180 },
      {
        prop: "dvalue",
        label: "dictionaryValue"
      },
      {
        prop: "status",
        label: "状态",
        enum: status1(),
        tag: true
      },
      { prop: "remark", label: "remark" }
    ],
    headButtonList: [
      {
        label: "add",
        type: "primary",
        icon: "Plus",
        methods: () => {
          if (!treeData.selectValue) {
            elMessage("请先选择左侧需要新增的明细数据", "warning");
            return;
          }
          const params = {
            dcode: treeData.selectValue,
            dvalue: ""
          };

          openForm<Dictionary.ReqSave>(addForm, "add", params);
        }
      }
    ],
    rowButtonList: [
      {
        label: "edit",
        type: "primary",
        methods: (row: Dictionary.ResGetByNames) => {
          console.log(row, "row");

          openForm(addForm, "add", row);
        },
        attrs: {
          link: true
        }
      },
      {
        label: "enable",
        type: "primary",
        hide: item => item.status === 1,
        methods: (row: Dictionary.ResGetByNames) => {
          const params = { id: row.id, status: 0 };
          useHandleData(dictionaryEnable, params, `${transitionLanguages("table", "enable")}【${row.dvalue}?`, refresh);
        },
        attrs: {
          link: true
        }
      },
      {
        label: "disabled",
        type: "danger",
        hide: item => item.status === 0,
        methods: (row: Dictionary.ResGetByNames) => {
          const params = { id: row.id, status: 1 };
          useHandleData(dictionaryDisable, params, `${transitionLanguages("table", "enable")}【${row.dvalue}?`, refresh);
        },
        attrs: {
          link: true
        }
      },
      {
        label: "delete",
        type: "danger",
        hide: row => row.status === 0,
        methods: (row: Dictionary.ResGetByNames) => {
          const name = `${transitionLanguages("button", "delete")}【${row.dvalue}】?`;
          useHandleData(dictionaryRemove, { id: row.id }, name, refresh);
        },
        attrs: {
          link: true
        }
      }
    ]
  });
  return {
    dataGrid
  };
}
